function xmlToJson(xml) {
	
	// Create the return object
	var obj = {};

	if (xml.nodeType == 1) { // element
		// do attributes
		if (xml.attributes.length > 0) {
		obj["data"] = {};
			var name, nexthop, network, cost,depth, mask;
			for (var j = 0; j < xml.attributes.length; j++) {
				depth = "undefined";
				var attribute = xml.attributes.item(j);
				obj["data"][attribute.nodeName] = attribute.nodeValue;
				if(attribute.nodeName == "network")
				{
					network = attribute.nodeValue;
				}
				else if(attribute.nodeName == "cost")
				{
					cost = attribute.nodeValue;
				}
				else if(attribute.nodeName == "nexthop")
				{
					nexthop = attribute.nodeValue;
				}
				else if(attribute.nodeName == "id")
				{
					obj["id"] = attribute.nodeValue;
				}
				else if(attribute.nodeName == "depth")
				{
					depth = attribute.nodeValue;
				}
				else if(attribute.nodeName == "mask")
				{
					mask = attribute.nodeValue;
				}
			}
			if(null != network && null != cost && null != nexthop)
			{
				if(depth==0){
					obj["name"] = network + "/" + mask;//+ " cost :" + cost;
				} else {
					obj["name"] = network + "/" + mask + " via " + nexthop + " cost :" + cost;
				}
			}
		}
	} else if (xml.nodeType == 3) { // text
		obj = xml.nodeValue;
		obj["data"] = xml.nodeValue;
	}

	// do children
	if (xml.hasChildNodes()) {
		for(var i = 0; i < xml.childNodes.length; i++) {
			var item = xml.childNodes.item(i);
			var nodeName = item.nodeName;
			if(nodeName == "learn"){nodeName = "children";}
			if(nodeName == "direct"){nodeName = "children";}			
			
			// if (typeof(obj[nodeName]) == "undefined") {				
				// obj[nodeName] = xmlToJson(item);
			// } else {
				// if (typeof(obj[nodeName].push) == "undefined") {				
					// var old = obj[nodeName];
					// obj[nodeName] = [];
					// obj[nodeName].push(old);
				// }
				// obj[nodeName].push(xmlToJson(item));
			// }
			
			if (typeof(obj[nodeName]) == "undefined" || typeof(obj[nodeName].push) == "undefined") {				
				var old = obj[nodeName];
				obj[nodeName] = [];
				obj[nodeName].push(old);
			}
			obj[nodeName].push(xmlToJson(item));	
		}
	}
	return obj;
}

